module ModularArithmetic

class Mod

    def initialize(modulus, value)
        @mod = modulus
        @val = value % @mod
    end
    
    def +(int)
        raise StandardError, "Moduluses must be equal: #{@mod} != #{int.modulus}" if @mod != int.modulus
        return Mod.new(@mod, @val + int.value)
    end
    
    def -(int)
        return self + Mod.new(int.modulus, -int.value)
    end
    
    def *(int)
        raise StandardError, "Moduluses must be equal: #{@mod} != #{int.modulus}" if @mod != int.modulus
        return Mod.new(@mod, @val * int.value)
    end
    
    def ==(int)
        raise StandardError, "Moduluses must be equal: #{@mod} != #{int.modulus}" if @mod != int.modulus
        return @val == int.value
    end
    
    def modulus
        @mod
    end
    
    def value
        @val
    end
    
    def inspect
        puts "#{@val}_#{@mod}"
    end

end

end


def mod(modulus, value)
    ModularArithmetic::Mod.new(modulus, value)
end

#puts (mod(12, 1) == mod(12,13)).inspect

m = 22
(0..m-1).each do |x|
    puts "(#{x})" if (mod(m, 4) * mod(m, x)) == mod(m, 26)
end


puts "--------"

m = 4
(0..m-1).each do |x|
    (0..m-1).each do |y|
        puts "(#{x}, #{y})" if (mod(m, x) + mod(m, 3) * mod(m, y)) == mod(m, 1)
    end
end

puts "--------"

m = 2
(0..m-1).each do |x|
    (0..m-1).each do |y|
        (0..m-1).each do |z|
            puts "(#{x}, #{y}, #{z})" if (mod(m, x) + mod(m, 3) * mod(m, y) - mod(m, z)) == mod(m, 1)
        end
    end
end

